﻿using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

namespace LocalizationUtils
{
    public class LocalizationManager
    {
        public enum CustomSystemLanguage
        {
            Chinese = SystemLanguage.Chinese,
            English = SystemLanguage.English,
            Korean = SystemLanguage.Korean,
            Japanese = SystemLanguage.Japanese,
            Portuguese = SystemLanguage.Portuguese, // 葡萄牙（葡萄牙）
            French = SystemLanguage.French,
            Spanish = SystemLanguage.Spanish,
            German = SystemLanguage.German,
            Russian = SystemLanguage.Russian,
            Italian = SystemLanguage.Italian,
            Turkish = SystemLanguage.Turkish,
            Polish = SystemLanguage.Polish,
            Thai = SystemLanguage.Thai,
            ChineseTraditional = SystemLanguage.ChineseTraditional,
            Vietnamese = SystemLanguage.Vietnamese,
            Indonesian = SystemLanguage.Indonesian,
            Arabic = SystemLanguage.Arabic,
            Romanian = SystemLanguage.Romanian,
            SimplifiedChinese = SystemLanguage.ChineseSimplified,
            Unknown = SystemLanguage.Unknown,

            //自定义语言
            Malaysian = 44, //马来语
            PortugueseBrazil = 45, //葡萄牙巴西
        }

        private static string s_currentLocale;

        public static readonly string DEFAULT_LOCALE = "en_us";

        public static readonly string ZH_LOCALE = "zh_cn";
        //private static IFileLoader s_fileLoader;

        private static readonly Dictionary<CustomSystemLanguage, string> s_lang2locale =
            new Dictionary<CustomSystemLanguage, string>()
            {
                { CustomSystemLanguage.Chinese, "zh_cn" },
                { CustomSystemLanguage.English, "en_us" },
                { CustomSystemLanguage.Korean, "ko_kr" },
                { CustomSystemLanguage.Japanese, "ja_jp" },
                { CustomSystemLanguage.Portuguese, "pt_pt" },
                { CustomSystemLanguage.French, "fr_fr" },
                { CustomSystemLanguage.Spanish, "es_es" },
                { CustomSystemLanguage.German, "de_de" },
                { CustomSystemLanguage.Russian, "ru_ru" },
                { CustomSystemLanguage.Italian, "it_it" },
                { CustomSystemLanguage.Turkish, "tr_tr" },
                { CustomSystemLanguage.Polish, "pl_pl" },
                { CustomSystemLanguage.Thai, "th_th" },
                { CustomSystemLanguage.ChineseTraditional, "zh_tw" },
                { CustomSystemLanguage.Vietnamese, "vi_vn" },
                { CustomSystemLanguage.Indonesian, "id_id" },
                { CustomSystemLanguage.Arabic, "ar_ae" },
                { CustomSystemLanguage.Romanian, "ro_ro" },
                { CustomSystemLanguage.Malaysian, "ms_my" },
                { CustomSystemLanguage.PortugueseBrazil, "pt_br" },
                { CustomSystemLanguage.SimplifiedChinese, "zh_cn" },
                /*{ CustomSystemLanguage.Afrikaans, "af_za" },
                { CustomSystemLanguage.Basque, "eu_es" },
                { CustomSystemLanguage.Belarusian, "be_by" },
                { CustomSystemLanguage.Bulgarian, "bg_bg" },
                { CustomSystemLanguage.Catalan, "ca_es" },
                { CustomSystemLanguage.Czech, "cs_cz" },
                { CustomSystemLanguage.Danish, "da_dk" },
                { CustomSystemLanguage.Dutch, "nl_nl" },
                { CustomSystemLanguage.Estonian, "et_ee" },
                { CustomSystemLanguage.Faroese, "fo_fo" },
                { CustomSystemLanguage.Finnish, "fi_fi" },
                { CustomSystemLanguage.Greek, "el_gr" },
                { CustomSystemLanguage.Hebrew, "he_il" },
                { CustomSystemLanguage.Hungarian, "hu_hu" },
                { CustomSystemLanguage.Icelandic, "is_is" },
                { CustomSystemLanguage.Latvian, "lv_lv" },
                { CustomSystemLanguage.Lithuanian, "lt_lt" },
                { CustomSystemLanguage.Norwegian, "no_no" },
                { CustomSystemLanguage.Romanian, "ro_ro" },
                { CustomSystemLanguage.SerboCroatian, "en_us" },
                { CustomSystemLanguage.Slovak, "sk_sk" },
                { CustomSystemLanguage.Slovenian, "sl_sl" },
                { CustomSystemLanguage.Swedish, "sv_se" },
                { CustomSystemLanguage.Ukrainian, "uk_ua" },
                { CustomSystemLanguage.Hindi, "hi_in" },*/
                { CustomSystemLanguage.Unknown, "en_us" },
            };

        public static CustomSystemLanguage GetSystemLanguageByLocale(string locale)
        {
            //Debug.Log("font GetSystemLanguageByLocale = " + locale);
            foreach (var item in s_lang2locale)
            {
                if (item.Value == locale)
                    return item.Key;
            }

            return GetSystemLanguage();
        }

        public static string CurrentLocale
        {
            get { return s_currentLocale; }
        }

        public static string Language2Locale(CustomSystemLanguage l)
        {
            return s_lang2locale[l];
        }

        public static CustomSystemLanguage CurrSystemLanguage
        {
            get { return GetSystemLanguageByLocale(s_currentLocale); }
        }

        public static void Init(string locale /*, IFileLoader fileLoader*/)
        {
            if (string.IsNullOrEmpty(locale))
            {
                locale = SettingManager.GetString("locale", true);
                Debug.LogFormat($"[localize] get local from setting {locale}");
                if (string.IsNullOrEmpty(locale))
                {
                    Debug.LogFormat($"[localize] try init use system language: {GetSystemLanguage()}");
                    // default system lang
                    string defaultLocale;
                    if (!s_lang2locale.TryGetValue(GetSystemLanguage(), out defaultLocale))
                    {
                        Debug.Log($"[localize] not found locale for system language:{GetSystemLanguage()}");
                        defaultLocale = DEFAULT_LOCALE;
                    }

                    locale = defaultLocale;
                    Debug.LogFormat($"[localize] init local use system: {GetSystemLanguage()} {locale}");

                    s_currentLocale = locale;
                    SettingManager.SetString("locale", locale, true);
                }
                else
                {
                    //这里不需要检查locale是否存在，否则如果热更支持了，但是apk包里可能不存在相应的文件
                    s_currentLocale = locale;
                }
            }
            else
            {
                s_currentLocale = locale;
                SettingManager.SetString("locale", locale, true);
            }
            //s_fileLoader = fileLoader;
            //LoadLocalize(s_fileLoader, locale);
        }

        public static void ChangeLocale(string locale)
        {
            Debug.Log("font ChangeLocale" + locale);
            //LoadLocalize(s_fileLoader, locale);
            s_currentLocale = locale;
            SettingManager.SetString("locale", locale, true);
        }

        public static CustomSystemLanguage GetSystemLanguage()
        {
            // 获取当前系统语言
            SystemLanguage currentLanguage = Application.systemLanguage;
            if (IsMalaysianLanguage())
            {
                return CustomSystemLanguage.Malaysian; // 新加坡语标识符
            }
            else if (IsPortugueseBrazil())
            {
                return CustomSystemLanguage.PortugueseBrazil;
            }
            else if (IsPortuguesePortugal())
            {
                return CustomSystemLanguage.Portuguese;
            }

            return (CustomSystemLanguage)currentLanguage;
        }

        static bool IsPortugueseBrazil()
        {
            // 获取系统的区域设置
            CultureInfo currentCulture = CultureInfo.CurrentCulture;

            // 检查区域设置的语言是否是葡萄牙语
            // 并且检查区域设置的国家/地区是否为巴西
            return currentCulture.Name.ToLower() == "pt-br";
        }

        static bool IsPortuguesePortugal()
        {
            // 获取系统的区域设置
            CultureInfo currentCulture = CultureInfo.CurrentCulture;

            // 检查区域设置的语言是否是葡萄牙语
            // 并且检查区域设置的国家/地区是否为葡萄牙
            return currentCulture.Name.ToLower() == "pt-pt";
        }

        static bool IsMalaysianLanguage()
        {
            // 获取系统的区域设置
            CultureInfo currentCulture = CultureInfo.CurrentCulture;

            // 检查区域设置的语言是否是马来西亚语
            // 马来西亚语的区域设置通常为"ms"
            return currentCulture.Name.ToLower() == "ms";
        }
    }
}